﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DefaultScope;
using Microsoft.Synchronization.ClientServices.IsolatedStorage;

namespace NorthwindClient
{
    public partial class MainPage : UserControl
    {

        //<CHANGED> - patrz tutorial

        DefaultScopeOfflineContext context;

        public MainPage()
        {
            // create an instance of offline context type
            // the code for this class is generated by the SyncSvcUtil utility.

            //jako piewrwszy argument mozna podac dowolna nazwe - jest z niej tworzone uri do cache'a przechowywujacego dane synchronizacji klienta ???
            //w drugim argumencie nie nalezy nic zmieniać
            context = new DefaultScopeOfflineContext("list_client",
                                    new Uri(new Uri(new WebClient().BaseAddress),
                                    "../DefaultScopeSyncService.svc/"));

            //to można wywalić, przydaje się w razie konflitów wersji cache'a, gdy się zmieniało coś w kodzie (np. dodawło nowe tabele do synchronizacji)
            context.ClearCache();

            // specify a value for the userid parameter for scope
            context.CacheController.ControllerBehavior.AddScopeParameters("userid",
                           new Guid("{EB9A4E37-95EB-4772-BA1E-84316BA26C8E}").ToString());

            // subscribe for the context.LoadCompleted event
            context.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(context_LoadCompleted);

            // subscribe for the Page.Loaded event 
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            // subscribe for the RefreshCompleted event
            context.CacheController.RefreshCompleted += (sender, args) =>
            {
                if (args.Error != null)
                {
                    Dispatcher.BeginInvoke(
                        delegate
                        {
                            // display any error occurred during refresh operation in a message box
                            MessageBox.Show(args.Error.ToString());
                        });
                }
            };

            InitializeComponent(); //jedyne nie dopisane ;)
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // when the page is loaded, invoke LoadAsync on the context object 
            // to load data from the cache into memory asynchronously. 
            // This method raises the LoadCompleted event at the end of load operation.

            context.LoadAsync();
        }

        void context_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            // bind the TagCollection object of context to ItemsSource to TagsGrid     
            Dispatcher.BeginInvoke(delegate
            {
                TagsGrid.ItemsSource = context.CustomersCollection;
            });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // saves any outstanding changes made by the application
            context.SaveChanges();

            // refreshes the cache by uploading all modified changes and then by downloading the server changes
            context.CacheController.RefreshAsync();
        }

        //</CHANGED>
    }
}


